import {g_UserList} from './g_UserList'
import store from '@/store'
import{xchat_ws} from '@/utils/xchat/xchat_ws'
import roomTemplate0 from '@/views/detail/template0/room_template0'
export class xMessager{
	constructor(uid,room_id,level,nickname,chatdisable,chatShow) {
	  this.uid = uid
		this.room_id = room_id
		this.level = level
		this.nickname = nickname
		this.chatdisable = chatdisable
		this.logined = false
		this.chatShow = chatShow
	}
	SocketError(xchat_restart){
		if (!this.logined){
		}
		else{
		}
		this.logined = false;
		var parma_arr = {};
// 		parma_arr._token = csrf_token;
// 		parma_arr.room_id = userpara.room_id;
		//重连webscoket
// 		var restart_func = function(){
// 			$.ajax({type: "POST", url:"/api/token/chat_token" , data: parma_arr, dataType : "json",
// 					success: function(res){
// 						if(res.error == 0){
// 							g_UserList.Clear();
// 							xchat_restart(res.chat_token);
// 						}
// 					},
// 					error : function(){
// 					}
// 			});
// 		}
// 		setTimeout(function(){
// 			restart_func();
// 		},3000);
	}
	OnPicMsg(chatdata){
		console.log('PicMsg')
	}
	OnC_ALL(data){
		console.log('OnC_ALL');
		console.log(data);
		var chatdata = {};
		chatdata.uid = data.uid;
		chatdata.message = data.message;
		chatdata.nickname = data.nickname;
		chatdata.secret = 1;
		chatdata.to_nickname = '我';
		chatdata.is_my_to = true
	}
	OnUPDATE_RECEIVE(data,roompara){
		console.log('OnUPDATE_RECEIVE');
		console.log(data);
		roompara.is_receive_share = data.is_receive_share;
	}
	OnChat(chatdata){
		let roompara = store.getters.roompara
		let room_id = store.getters.roompara.room_id
		if(room_id != chatdata.frid && !chatdata.secret){
			if(roompara.is_receive_share == 0){
				return false;
			}
			if(roompara.is_receive_share == 1 && roompara.parent_room_id != chatdata.frid){
				return false;
			}
		}
		var is_my_message = false;
		if (chatdata.from_uid == this.uid){
			is_my_message = true;
			chatdata.nickname = "我";
			chatdata.is_my = true;
		}
		if (chatdata.touid == this.uid){
			is_my_message = true;
			chatdata.to_nickname = "我";
			chatdata.is_my_to = true;
		}
		if(!chatdata.nickname){
			chatdata.nickname = chatdata.from_nickname
		}
		//私聊
		if(chatdata.secret){
			store.dispatch('setPriTips',true)
			xchat_ws.privateChat.private_chat_display_chatmessage(chatdata,is_my_message)
		}else{
			xchat_ws.publicChat.public_chat_display_chatmessage(chatdata,is_my_message)
		}
	}
	OnGift(data){
		var is_my_message = false;
		if (data.from_uid == this.uid || data.to_uid == this.uid){
			is_my_message = true;
		}
		data.fromu=g_UserList.GetUser(data.from_uid)
		if(data.to_uid){
			data.tou=g_UserList.GetUser(data.to_uid);
		}
		// message_display.gift(data,is_my_message);
	}
	OnFreeFlower(data,roompara){
		let room_id = store.getters.roompara.room_id
		if(room_id != data.frid){
			if(roompara.is_receive_share == 0){
				return false;
			}
			if(roompara.is_receive_share == 1 && roompara.parent_room_id != data.frid){
				return false;
			}
		}
		var is_my_message = false;
		if (data.fromwho.uid == this.uid || data.sendwho.uid == this.uid){
			is_my_message = true;
		}
		xchat_ws.publicChat.freeFlower(data)
	}
	OnNotifyRelay(data){
		console.log('OnNotifyRelay');
	}
	OnPublic_Msg_Del(data){
		console.log('OnPublic_Msg_Del');
	}
	OnLinechange(data){
		console.log(data.default_line);
		//触发线路切换
		roompara.default_line = data.default_line;
		if(data.default_line == '-1'){
			// $(".tan-line .cont p span[dataid='"+watch_serverReinit[0].id+"']").click()
		}else{
			// $(".tan-line .cont p span[dataid='"+data.default_line+"']").click();
		}
	}
	OnData(data){
		console.log('OnData');
		switch (data.type){
			case 'C':
				this.OnChat(data.data);
				break;
			case 'F':
				this.OnFreeFlower(data.data);
				break;
			case 'G':
				this.OnGift(data.data);
				break;
			case 'C_ALL':
				this.OnC_ALL(data.data);
				break;
			case 'PIC_MSG':
				this.OnPicMsg(data.data);
				break;
			case 'PUBLIC_MSG_DEL':
				this.OnPublic_Msg_Del(data.data);
				break;
			case 'UPDATE_RECEIVE':
				this.OnUPDATE_RECEIVE(data.data);
				break;
			case 'NOTIFY_RELAY':
				this.OnNotifyRelay(data.data);
				break;
			case 'DEFAULT_LINE':
				this.OnLinechange(data.data);
				break;
		}
	}

	freeflower(){
		//if (this.level<100){
		//	checklogin();
		//	return;
		//}

		var my_u = g_UserList.GetUser(this.uid);
		//titleLiClick($('.tabchat .tabtitle span'),0);
		if (my_u){
			var parma_arr = {};
// 			parma_arr._token = csrf_token;
// 			parma_arr.to_uid = g_UserList.selected_uid;
// 			parma_arr.room_id = room_id;
			// var del_div = layer.open({type: 2});
// 			$.ajax({type: "POST", url:version+"/room/room_send_flower.html" , data: parma_arr, dataType : "json",
// 				success: function(res){
// 					layer.close(del_div);
// 					if(res.error == 0){
// 						//layer.msg(res.message, {icon: 6});
// 					}else{
// 						layer.open({content: res.message, time: 2});
// 					}
// 				},
// 				error : function(){
// 					layer.close(del_div);
// 					layer.open({content: '操作发生错误！', time: 2});
// 				}
// 			});
		}
	}

	disablechat(){
		var mins = 5;
		var from_u = g_UserList.GetUser(this.uid);
	}

	ReEnter(){
		console.log('onReEnter');
		this.logined = false;
		// message_display.prv('<span class="warning">你已被同一账号挤出本房间！</span>');
	}
	
	LoginACK(param){
		if (param.loginack==0){
			this.logined = false;
			switch (param.reason){
				case 2:
					// message_display.pub('<span class="warning">服务器人数满了！</span>');
				break;
				case 3:
					// message_display.pub('<span class="warning">房间人数满了！</span>');
				break;
				case 5:
					// message_display.pub('<font class="warning">账号已经在其他页面登录本房间！</font>',1);
				break;
			}
		}
	}
	get Logined(){
		return this.logined
	}
	set Logined(val){
		this.logined = val
	}
	get Chatdisable(){
		return this.chatdisable
	}
	set Chatdisable(val){
		this.chatdisable = val
	}
}
